package com.xltt.vehicleControl.activity;


import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.xltt.vehicleControl.R;
import com.xltt.vehicleControl.base.BaseActivity;
import com.xltt.vehicleControl.fragment.VehicleFunctionFragment;
import com.xltt.vehicleControl.util.Logger;
import com.xltt.vehicleControl.util.SharedPreferencesUtils;
import com.xltt.vehicleControl.view.SubmitButton;

import butterknife.BindView;
import butterknife.OnClick;

public class AcSettingsActivity extends BaseActivity {

    @BindView(R.id.ac_settgs_btn)
    SubmitButton mAcSettingsBtn;
    @BindView(R.id.ac_close_btn)
    SubmitButton mAcCloseBtn;
    @BindView(R.id.ac_add_btn)
    ImageView mAcAddBtn;
    @BindView(R.id.ac_remove_btn)
    ImageView mAcRemoveBtn;
    @BindView(R.id.ac_value_txt)
    TextView mAcValue;

    @BindView(R.id.ac_add_time_btn)
    ImageView mAcAddTimeBtn;
    @BindView(R.id.ac_remove_time_btn)
    ImageView mAcRemoveTimeBtn;
    @BindView(R.id.ac_value_time_txt)
    TextView mAcTimeValue;

    private static final int AC_TIME_MAX_VALUE = 10;
    private static final int AC_TIME_MIN_VALUE = 3;
    private static final int AC_MAX_VALUE = 32;
    private static final int AC_MIN_VALUE = 16;
    private static final String AC_KEY = "ac_key";
    private static final String AC_TIME_KEY = "ac_time_key";
    private static final int[] ac_time_value_key = new int[]{3,5,8,10};

    @OnClick({R.id.ac_settgs_btn,R.id.ac_add_btn,R.id.ac_remove_btn,R.id.ac_close_btn,R.id.ac_add_time_btn,R.id.ac_remove_time_btn})
    public void onViewClicked(View view) {
        String value = mAcValue.getText().toString();
        String acTimeValue = mAcTimeValue.getText().toString();
        //1.关闭AC
        if(view == mAcCloseBtn){
            Intent intent = new Intent();
            intent.putExtra(VehicleFunctionFragment.COMMAND_PARAMETER,"stop");
            setResult(VehicleFunctionFragment.AC_CLOSE_RESULT_CODE,intent);
            finish();
        }else if(view == mAcSettingsBtn){
            Intent intent = new Intent();
            intent.putExtra(VehicleFunctionFragment.AC_PARAMETER,value);
            intent.putExtra(VehicleFunctionFragment.TIME_PARAMETER,acTimeValue);
            intent.putExtra(VehicleFunctionFragment.COMMAND_PARAMETER,"start");
            setResult(VehicleFunctionFragment.AC_SETTINGS_RESULT_CODE,intent);
            finish();
        }else if(view == mAcAddBtn){
            int addValue = Integer.valueOf(value) + 1;
            if(addValue > AC_MAX_VALUE){
            }else{
                mAcValue.setText(addValue + "");
            }
        }else if(view == mAcRemoveBtn){
            int removeValue = Integer.valueOf(value) - 1;
            if(removeValue < AC_MIN_VALUE){
            }else{
                mAcValue.setText(removeValue + "");
            }
        }else if(view == mAcAddTimeBtn){
            int addValue = Integer.valueOf(acTimeValue) + 1;
            if(addValue < 5){
                addValue = 5;
            }else if(addValue >= 5 && addValue < 8){
                addValue = 8;
            }else if(addValue >= 8 && addValue < 10){
                addValue = 10;
            }else{
                addValue = 10;
            }
            mAcTimeValue.setText(addValue + "");
        }else if(view == mAcRemoveTimeBtn){
            int removeValue = Integer.valueOf(acTimeValue) - 1;
            if(removeValue < 5){
                removeValue = 3;
            }else if(removeValue >= 5 && removeValue < 8){
                removeValue = 5;
            }else if(removeValue >= 8 && removeValue < 10){
                removeValue = 8;
            }else{
                removeValue = 3;
            }
            mAcTimeValue.setText(removeValue + "");
        }
        String saveValue = mAcValue.getText().toString();
        String saveTimeValue = mAcTimeValue.getText().toString();
        Logger.d("保存空调温度 = " + saveValue + ",时间 = " + saveTimeValue);
        SharedPreferencesUtils.setParam(this,AC_KEY,saveValue);
        SharedPreferencesUtils.setParam(this,AC_TIME_KEY,saveTimeValue);
    }

    @Override
    protected void initPresenter() {

    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_ac_settings;
    }

    @Override
    protected void initViews() {
        Object defaultValue = SharedPreferencesUtils.getParam(this,AC_KEY,24 + "");
        mAcValue.setText(defaultValue + "");
        Object defaultTimeValue = SharedPreferencesUtils.getParam(this,AC_TIME_KEY,3 + "");
        mAcTimeValue.setText(defaultTimeValue + "");
        Logger.d("空调温度 = " + defaultValue.toString() + "，空调启动时长 = " + defaultTimeValue.toString());
    }


}
